﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BGSpaceShooter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BGSpaceShooterServer
{
    internal class ProjectileController
    {
        ProjectileContainer ProjectileContainer = new ProjectileContainer();
        private const double Cooldown = 500; //Cooldown broni w ms

        public IEnumerable<Projectile> PrepareDebris(int numberOfAsteroids)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            List<Projectile> asteroidsIds = new List<Projectile>(numberOfAsteroids);
            for (int i = 0; i < numberOfAsteroids; i++)
            {
                int sparse = numberOfAsteroids * 20;
                Projectile createdAsteroid = ProjectileContainer.CreateAsteroid(new Vector3(random.Next(sparse) - random.Next(sparse), random.Next(sparse) - random.Next(sparse), random.Next(sparse) - random.Next(sparse)));
                if (createdAsteroid != null)
                    asteroidsIds.Add(createdAsteroid);
            }
            return asteroidsIds;
        }

        public IEnumerable<Projectile> PrepareDebrisForPalyer(IEnumerable<Player> players)
        {
            List<Projectile> asteroidsIds = new List<Projectile>(players.Count());
            Random random = new Random(DateTime.Now.Millisecond);
            foreach (Player player in players)
            {
                if (player.LastInput.Contains(Keys.P))
                {
                        int sparse = 15;
                        Projectile createdAsteroid =
                            ProjectileContainer.CreateAsteroid(
                                new Vector3(random.Next(sparse)  +5+ player.StarFighter.Position.X,
                                            random.Next(sparse)  + 5+player.StarFighter.Position.Y,
                                            random.Next(sparse) +5 + player.StarFighter.Position.Z));
                    
                        if (createdAsteroid != null)
                        {
                            asteroidsIds.Add(createdAsteroid);
                            createdAsteroid.Velocity = new Vector3(0);
                        }
                            
                }

            }
            return asteroidsIds;
        }


        public IEnumerable<ServerStatusResponse> GetProjectileCreationStatus(IEnumerable<Projectile> projectiles)
        {
            List<ServerStatusResponse> responses = new List<ServerStatusResponse>(projectiles.Count());
            foreach (Projectile projectile in projectiles)
            {
                if (projectile.IsAlive)
                {
                    ServerStatusResponse response = new ServerStatusResponse();
                    response.playerId = projectile.Id;  //sic!
                    response.GameObjectName = projectile.ProjectileType;
                    response.gameObjectType = GameObjectType.Projectile;
                    response.Position = projectile.Position;
                    response.Rotation = projectile.Rotation;
                    response.Velocity = projectile.Velocity;
                    responses.Add(response);
                }
            }
            return responses;
        }

        public IEnumerable<ServerStatusResponse> GetProjectileDestructionStatus(IEnumerable<Projectile> projectiles)
        {
            List<ServerStatusResponse> responses = new List<ServerStatusResponse>(projectiles.Count());
            foreach (Projectile projectile in projectiles)
            {
                ServerStatusResponse response = new ServerStatusResponse();
                response.playerId = projectile.Id;  //sic!
                response.GameObjectName = projectile.ProjectileType;
                response.gameObjectType = GameObjectType.Projectile;
                responses.Add(response);
            }
            return responses;
        }


        public IEnumerable<ServerStatusResponse> CreateProjectilesForPlayers(IEnumerable<Player> players)
        {
            List<Projectile> projectiles = new List<Projectile>();
            DateTime creation = DateTime.Now;
            foreach (Player player in players)
            {
                if ((DateTime.Now - player.LastProjectileCreation).TotalMilliseconds >= Cooldown && player.LastInput.Contains(Keys.Space))
                {
                    Projectile bullet = ProjectileContainer.CreateBullet(player.StarFighter, player.PlayerID, 0, creation);
                    if (bullet != null)
                    {
                        player.LastProjectileCreation = creation;
                        projectiles.Add(bullet);
                    }
                    bullet = ProjectileContainer.CreateBullet(player.StarFighter, player.PlayerID, 1, creation);
                    if (bullet != null)
                    {
                        player.LastProjectileCreation = creation;
                        projectiles.Add(bullet);
                    }
                }
            }
            return GetProjectileCreationStatus(projectiles);
        }

        /// <summary>
        /// Usuwa pociski starsze niz zadana wartosc. Rozpatrywane w sekundach 
        /// </summary>
        public void DisposeOldProjectiles()
        {
            ProjectileContainer.ClearFromOldProjectiles();
        }

        public void UpdateProjectiles()
        {
            ProjectileContainer.UpdateProjectiles();
        }

        public IEnumerable<Projectile> GetActiveProjectiles()
        {
            return ProjectileContainer.GetAcitveProjectiles();
        }

        /// <summary>
        /// Pobiera liste zombie pociskow i likwiduje ich oznaczenie jako zombie
        /// </summary>
        /// <returns>Lista zombie pociskow</returns>
        public IEnumerable<Projectile> GetZombieProjectiles()
        {
            return ProjectileContainer.GetZombieProjectiles();
        }
    }


}
